home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Calling the wrong constructor
- Date: Sat, 20 Apr 1996 15:10:08 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4laun2$4vm@news.halcyon.com>
- References: <4kot87$796@earth.njcc.com> <Andrew_Carol-1804962001240001@17.127.18.252>
- NNTP-Posting-Host: blv-pm2-ip16.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Andrew_Carol@quickmail.apple.com (Andrew) wrote:
-
- >In article <4kot87$796@earth.njcc.com>, mike@pluto.njcc.com (Michael
- >Hohenshilt) wrote:
-
- >> This might seem like a basic question, but lets say you something set u
- >> like the following:
- >>
- >> class Parent { Parent() { allocsomething} ~Parent() { deletesomething }... };
- >> class Foo : Parent { Foo() { allocsomething } ~Foo() {deletesomething }... };
- >> class Contain { Parent *ptr; ... };
- >>
- >> both parent, and foo allocate memory, and will free it up when being
- >> destructed. Contain just holds a pointer of type Parent, and its
- >> constructor takes such a pointer as a parameter.
- >> If I store a pointer of type Foo into contain.ptr and discard that
- >> pointer. Is there any way to have contain (via destructors) free up all
- >> memory allocated by Foo and/or Parent?
-
-
- >Assuming that "Contain" keeps a copy of the 'ptr'...
-
- >~Contain() { delete ptr; }
-
- >This will call the destructor of Foo and Parent...
-
- >Good luck!
-
- No, it won't.
- Your Parent class needs virtual ~Parent(). Then it will.
-
- IMHO, you can never be sure how people after you will maintain your
- code, perhaps deriving classes from your own and calling functions
- that depend on polymorphism. Therefore, always make your destructor
- virtual.
-
- If you don't want the overhead of a vtable per object, make this
- decision not to use virtuals a well published, much heralded fact in
- your header. (Again, IMHO).
-
- --Norm
-
-